package com.jt.www.model.entity;

import com.jt.www.exception.BizException;
import com.jt.www.util.CalculateUtils;
import com.jt.www.util.FileUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.Table;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Table(name = "t_file_recorde")
public class FileRecordEntity extends BaseEntity {

    public static final String baseurl = "/files/downloadfile/";

    private String url;

    private String name;

    private String size;

    private String suffix;

    private Integer sourceType;

    private String createBy;

    private Boolean isDelete;

    public static List<FileRecordEntity> convertToFileRecorde(HashMap<String, Long> fileNameAndFileId,
                                                              MultipartFile[] files, Integer sourceType, String userId) {
        List<FileRecordEntity> fileRecordEntitys = new ArrayList<>();
        Date now = new Date();
        for (MultipartFile multipartFile : files) {
            String fileName = multipartFile.getOriginalFilename();
            Long fileId = fileNameAndFileId.get(fileName);
            if (fileId == null) {
                throw new BizException("远程保存找不到对应的文件");
            }
            FileRecordEntity fileRecordEntity = new FileRecordEntity();
            fileRecordEntity.setUrl(baseurl + fileId);
            fileRecordEntity.setName(fileName);
            fileRecordEntity.setSize(CalculateUtils.caculateSiz(multipartFile.getSize()));
            fileRecordEntity.setSuffix(FileUtils.getSuffix(fileName));
            fileRecordEntity.setSourceType(sourceType);
            fileRecordEntity.setCreateBy(userId);
            fileRecordEntity.setIsDelete(false);
            fileRecordEntity.setCreateTime(now);
            fileRecordEntity.setUpdateTime(now);
            fileRecordEntitys.add(fileRecordEntity);
        }
        return fileRecordEntitys;
    }

    public static String getBaseurl() {
        return baseurl;

    }

    public String getUrl() {
        return this.url;

    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getName() {
        return this.name;

    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSize() {
        return this.size;

    }

    public void setSize(String size) {
        this.size = size;
    }

    public String getSuffix() {
        return this.suffix;

    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }

    public Integer getSourceType() {
        return this.sourceType;

    }

    public void setSourceType(Integer sourceType) {
        this.sourceType = sourceType;
    }

    public String getCreateBy() {
        return this.createBy;

    }

    public void setCreateBy(String createBy) {
        this.createBy = createBy;
    }

    public Boolean getIsDelete() {
        return this.isDelete;

    }

    public void setIsDelete(Boolean delete) {
        this.isDelete = delete;
    }
}